/*
 * Copyright (c) 2017. 深圳一七科技. All rights reserved.
 */

package com.yiqi.liebang.feature.mine.presenter;

import android.text.TextUtils;

import com.suozhang.framework.utils.DateUtil;
import com.suozhang.framework.utils.T;
import com.yiqi.liebang.entity.bo.EducationBo;
import com.yiqi.liebang.feature.mine.contract.EducationContract;
import com.yiqi.liebang.feature.mine.dependencies.edu.DaggerEduPresenterComponent;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * LIJUWEN
 *
 * @email yiyayiyayaoljw@gmail.com
 * @date 2018/7/11 9:31
 */
public class EduPresenter implements EducationContract.Presenter {

    private EducationContract.View mView;

    @Inject

    EducationContract.Model mModel;


    public EduPresenter(EducationContract.View mView) {
        this.mView = mView;
        DaggerEduPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void userAddEducation(String eduLogo, String schoolName, String subjectName, String diploma, String beginTime, String endTime, String eduDescribe) {
        if (TextUtils.isEmpty(schoolName)) {
            mView.showError("请学校名称！");
            return;
        }

        if (TextUtils.isEmpty(eduLogo)) {
            mView.showError("请选择学校LOGO！");
            return;
        }

        if (TextUtils.isEmpty(subjectName)) {
            mView.showError("请输入职位名称！");
            return;
        }
        if (TextUtils.isEmpty(diploma)) {
            mView.showError("请选择学历！");
            return;
        }
        if (TextUtils.isEmpty(beginTime)) {
            mView.showError("请选择开始时间！");
            return;
        }
        if (TextUtils.isEmpty(endTime)) {
            mView.showError("请选择结束时间！");
            return;
        }
        if (DateUtil.getTimeCompareSize(beginTime,
                endTime) !=3) {
            mView.showError("结束时间必须大于开始时间！");
            return;
        }
        if (TextUtils.isEmpty(eduDescribe)) {
            mView.showError("请添加教育经历描述！");
            return;
        }
        mModel.userAddEducation(eduLogo, schoolName, subjectName, diploma,beginTime, endTime, eduDescribe).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.showAddEducationSuccess();
//                T.showShort("添加成功!");
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void userEditEducation(String id, String eduLogo, String schoolName, String subjectName, String diploma, String beginTime, String endTime, String eduDescribe) {

        if (TextUtils.isEmpty(id)) {
            return;
        }

        if (TextUtils.isEmpty(schoolName)) {
            mView.showError("请学校名称！");
            return;
        }
        if (TextUtils.isEmpty(subjectName)) {
            mView.showError("请输入职位名称！");
            return;
        }
        if (TextUtils.isEmpty(diploma)) {
            mView.showError("请选择学历！");
            return;
        }
        if (TextUtils.isEmpty(beginTime)) {
            mView.showError("请选择开始时间！");
            return;
        }
        if (TextUtils.isEmpty(endTime)) {
            mView.showError("请选择结束时间！");
            return;
        }
        if (DateUtil.getTimeCompareSize(beginTime,
                endTime) !=3) {
            mView.showError("结束时间必须大于开始时间！");
            return;
        }
        if (TextUtils.isEmpty(eduDescribe)) {
            mView.showError("请添加教育经历描述！");
            return;
        }
        mModel.userEditEducation(id,eduLogo, schoolName, subjectName, diploma,beginTime, endTime, eduDescribe).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.showAddEducationSuccess();
//                T.showShort("修改成功!");
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void userGetEducation(String id) {
        mModel.userGetEducation(new EducationBo(id)).compose(mView.bindToLife()).subscribe(new Observer<EducationBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(EducationBo s) {
                mView.showEducationSuccess(s);
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void userDeleteEducation(String id, int sign) {
        mModel.userDeleteEducation(new EducationBo(id,sign)).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.showAddEducationSuccess();
//                T.showShort("删除成功!");
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }
}
